package org.esprit.ocm.client.ui.ec2;

import java.util.List;

import org.esprit.ocm.client.model.ec2.datasource.GroupLocalDS;
import org.esprit.ocm.client.rpc.ec2.SecurityGroupService;
import org.esprit.ocm.client.rpc.ec2.SecurityGroupServiceAsync;
import org.esprit.ocm.dto.ec2.impl.GroupDescriptionMappingDto;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public class GroupZone extends SectionStack {

	private final SecurityGroupServiceAsync	securityGroupService	= GWT.create(SecurityGroupService.class);

	private DataSource						ds;
	private ListGrid						grid;
	private ListGrid						gridPerm;

	public GroupZone(final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {

		getData(server, credentials);

		// creer groupSection
		SectionStackSection groupSection = new SectionStackSection("Security Groups List");
		groupSection.setExpanded(true);
		groupSection.setCanCollapse(false);
		addSection(groupSection);
		
		// creer detailSection
		SectionStackSection groupdetailSection = new SectionStackSection("Security Groups Permissions");
		groupdetailSection.setExpanded(true);
		groupdetailSection.setCanCollapse(false);
		addSection(groupdetailSection);

		// creer groupGrid
		grid = new ListGrid();
		setVisibilityMode(VisibilityMode.MULTIPLE);
		setHeaderHeight(20);
		// instanceGrid.setShowFilterEditor(true);
		grid.setFilterOnKeypress(true);
		grid.setAutoFetchData(true);
		
		// creer groupGridPermission
		gridPerm = new ListGrid();
		setVisibilityMode(VisibilityMode.MULTIPLE);
		setHeaderHeight(20);
		gridPerm.setFilterOnKeypress(true);
		gridPerm.setAutoFetchData(true);

		// creer grouptoolstrip
		ToolStrip groupGridToolStrip = new ToolStrip();
		groupGridToolStrip.setWidth100();
		groupGridToolStrip.addFill();

		// ajouter toolstrip a instanceSection
		groupSection.addItem(groupGridToolStrip);

		final Button addGroupButton = new Button("Add Group");
		addGroupButton.setShowRollOver(true);
		addGroupButton.setShowDown(true);
		addGroupButton.setIcon("group.png");
		addGroupButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new AddGroup(server, credentials, user);
			}
		});
		
		final Button deleteGroupButton = new Button("Remove");
		deleteGroupButton.setShowRollOver(true);
		deleteGroupButton.setShowDown(true);
		deleteGroupButton.setIcon("group.png");
		deleteGroupButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String name = grid.getSelectedRecord().getAttribute("name");
				System.out.println(name);
				securityGroupService.deleteSecurityGroup(name, server, credentials, new AsyncCallback<Boolean>() {
					
					public void onSuccess(Boolean b) {
						System.out.println(b);
						SC.say("deleted");
						ds.destroy();
						getData(server, credentials);
						grid.redraw();
					}
					
					public void onFailure(Throwable t) {
						// TODO Auto-generated method stub
						System.out.println(t);
					}
				});
			}
		});

		final Button permissionGroupButton = new Button("Permissions");
		permissionGroupButton.setShowRollOver(true);
		permissionGroupButton.setShowDown(true);
		permissionGroupButton.setIcon("permission.png");
		permissionGroupButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String id = grid.getSelectedRecord().getAttribute("name");
				new AddGroupPermissions(server, credentials, user, id);
			}
		});

		final Button refreshButton = new Button("Refresh");
		refreshButton.setShowRollOver(true);
		refreshButton.setShowDown(true);
		refreshButton.setIcon("interact.png");
		refreshButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SC.say("Refreshed");
				ds.destroy();
				getData(server, credentials);
				grid.redraw();
			}
		});
		
		Img filter = new Img("filter.png", 15, 15);

		// creer filterButton
		final Button filterButtonOn = new Button("On");
		filterButtonOn.setWidth(25);
		filterButtonOn.setHeight(20);
		filterButtonOn.setShowRollOver(true);
		filterButtonOn.setShowDown(true);
		// filterButtonOn.setIcon("filter.png");
		filterButtonOn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				grid.setShowFilterEditor(true);
				grid.setFilterOnKeypress(true);
			}
		});
		// creer filterButton
		final Button filterButtonOff = new Button("Off");
		filterButtonOff.setWidth(25);
		filterButtonOff.setHeight(20);
		filterButtonOff.setShowRollOver(true);
		filterButtonOff.setShowDown(true);
		// filterButtonOff.setIcon("filter.png");
		filterButtonOff.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				grid.setShowFilterEditor(false);
				grid.setFilterOnKeypress(false);
			}
		});

		// ajouter le bouton
		groupGridToolStrip.addMember(addGroupButton);
		groupGridToolStrip.addMember(deleteGroupButton);
		groupGridToolStrip.addMember(permissionGroupButton);
		groupGridToolStrip.addMember(refreshButton);
		groupGridToolStrip.addSpacer(10);
		groupGridToolStrip.addMember(filter);
		groupGridToolStrip.addSpacer(2);
		groupGridToolStrip.addMember(filterButtonOn);
		groupGridToolStrip.addMember(filterButtonOff);
		groupGridToolStrip.addSpacer(5);

		// affecter les fields de instanceGrid

//		ListGridField idField = new ListGridField("ID", 200);
//		ListGridField descriptionField = new ListGridField("Description");
//
//		grid.setFields(idField, descriptionField);

		// ajouter instanceGrid a instanceSection
		groupSection.addItem(grid);
		groupdetailSection.addItem(gridPerm);

		draw();
	}

	private void getData(ServerDto server, AwsCredentialsDto credentials) {
		securityGroupService.listSecurityGroups(server, credentials, new AsyncCallback<List<GroupDescriptionMappingDto>>() {

			public void onSuccess(List<GroupDescriptionMappingDto> list) {

				ds = GroupLocalDS.getInstance(list);
				grid.setDataSource(ds);
			}

			public void onFailure(Throwable arg0) {
			}
		});
	}
}
